package com.javaee.controller;


import com.javaee.service.ActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.javaee.entity.Activity;
import java.util.List;

@RestController
@RequestMapping("/Activities")
public class ActivitiesController {

    @Autowired
    private ActivitiesService activitiesService;

    @PostMapping
    public Result create(@RequestBody Activity activity) {
        boolean flag = activitiesService.create(activity);
        String msg = flag ? "创建成功" : "创建失败，请重试！";
        return new Result(flag ? Code.ADD_OK:Code.ADD_ERR,flag,msg);
    }

    @PutMapping()
    public Result update(@RequestBody Activity activity) {
        boolean flag = activitiesService.update(activity);
        String msg = flag ? "更新成功" : "更新失败，请重试！";
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag,msg);
    }

    @PutMapping("/{id}")
    public Result publish(@PathVariable Integer id) {
        boolean flag = activitiesService.publish(id);
        String msg = flag ? "发布成功" : "发布失败，请重试！";
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag,msg);
    }

    @GetMapping("/status/{status}")
    public Result getByStatus(@PathVariable Integer status) {
        List<Activity> activityList = activitiesService.getByStatus(status);
        Integer code = activityList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = activityList != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code,activityList,msg);
    }

    @GetMapping("/name/{name}")
    public Result getAll(@PathVariable String name) {
        List<Activity> activityList = activitiesService.getByName(name);
        Integer code = activityList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = activityList != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code,activityList,msg);
    }
}
